package com.aws.easyexcel;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.transfer.TransferManager;
import com.amazonaws.services.s3.transfer.Upload;
import org.apache.commons.collections4.ListUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @description:
 * @author: libugu@cskaoyan.com
 * @date 2024/1/16 - 15:53
 */
@Component
public class TestEasyDemo {
    String filePath = "C:\\Users\\libugu\\Desktop\\日常\\game\\CE7.3+CT整合\\CT表整合.zip";
    @Autowired
    AmazonS3 amazonS3;

    /**
     * RiskThreadTask
     * asynExportExcelFile
     */
    public void asynExportExcelFile(){
        String bucketName = "mylibugu";
        String key = "execl/a/" + "333" + System.currentTimeMillis() + ".xlsx";

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        EasyExcel.write(outputStream, DemoData.class).sheet("模板").doWrite(data());

        InputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());

        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setContentLength(outputStream.size());

        amazonS3.putObject(bucketName, key, inputStream, objectMetadata);

        System.out.println("execl上传完成");

    }


    /**
     * BsgRiskIcrPowerServiceImpl
     * saveFileToTemporarDir
     */
    public String saveFileToTemporarDir() throws FileNotFoundException, InterruptedException {

        String bucketName = "mylibugu";
        String key = "execl/a/" + "666" + System.currentTimeMillis() + ".zip";
        File file = new File(filePath);
        FileInputStream fileInputStream = new FileInputStream(file);
        TransferManager transferManager = new TransferManager(amazonS3);
        Upload upload = transferManager.upload(bucketName, key, fileInputStream, null);
        upload.waitForUploadResult();
        transferManager.shutdownNow();

        String url = "https://" + bucketName + ".s3.amazonaws.com/" + key;
        return url;

    }





//    public static void main(String[] args) {
//
////        // 写法2
//       String fileName =  "simpleWrite" + System.currentTimeMillis() + ".xlsx";
//        // 这里 需要指定写用哪个class去写，然后写到第一个sheet，名字为模板 然后文件流会自动关闭
//        // 如果这里想使用03 则 传入excelType参数即可
//        EasyExcel.write(fileName, DemoData.class).sheet("模板").doWrite(data());
//
//
//    }
    private static List<DemoData> data() {
        List<DemoData> list =new ArrayList<>();
        for (int i = 0; i < 1000000; i++) {
            DemoData data = new DemoData();
            data.setString("字符串" + i);
            data.setDate(new Date());
            data.setDoubleData(0.56);
            list.add(data);
        }
        return list;
    }
}
